package map.Bellman_ford;

import java.util.*;

class Edge2 {
    int from; //出发点
    int to; //邻接节点
    int val; //边权值

    public Edge2(int from, int to, int val) {
        this.from = from;
        this.to = to;
        this.val = val;
    }
}
public class Main2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        List<List<Edge3>> grid = new ArrayList<>();
        for (int i = 0; i <= n; i++) {
            grid.add(new ArrayList<>());
        }
        for (int i = 0; i < m; i++) {
            int s = sc.nextInt();
            int t = sc.nextInt();
            int val = sc.nextInt();
            grid.get(s).add(new Edge3(s, t, val));
        }
        int start = 1;
        int[] minDist = new int[n + 1];
        Arrays.fill(minDist, Integer.MAX_VALUE);
        minDist[start] = 0;
        Queue<Integer> queue = new LinkedList<>();
        boolean[] isInQueue = new boolean[n + 1];
        queue.add(start);
        while (!queue.isEmpty()){
            int cur = queue.poll();
            isInQueue[cur] = false;
            for (Edge3 edge : grid.get(cur)) {
                if (minDist[edge.from] + edge.val < minDist[edge.to]){
                    minDist[edge.to] = minDist[edge.from] + edge.val;
                    if (!isInQueue[edge.to]){
                        queue.offer(edge.to);
                        isInQueue[edge.to] = true;
                    }
                }
            }
        }
        if (minDist[n] == Integer.MAX_VALUE) {
            System.out.println("unconnected");
        } else {
            System.out.println(minDist[n]);
        }
    }
}
